package sunlight.util.config;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.InvalidPropertiesFormatException;

import sunlight.core.Application;

public class Configuration {
	
	public static int READER_TYPE_LOCAL 			= 1;
	public static int READER_TYPE_REMOTE 			= 2;
	public static int READER_TYPE_LOCAL_ENCRYPTED 	= 3;
	public static int READER_TYPE_REMOTE_ENCRYPTED 	= 4;
	
	protected static int Type = 0;
	
	public static void setReaderType(int readerType){
		Configuration.Type = readerType;
	}
	
	public static ConfigurationFile getReader() throws InvalidPropertiesFormatException, FileNotFoundException, IOException{
		if(Configuration.Type == Configuration.READER_TYPE_LOCAL){
			return Configuration.getLocalFile();
		}
		
		if(Configuration.Type == Configuration.READER_TYPE_LOCAL_ENCRYPTED){
			return Configuration.getEncryptedLocalFile();
		}
		
		if(Configuration.Type == Configuration.READER_TYPE_REMOTE){
			return Configuration.getRemoteFile();
		}
		
		if(Configuration.Type == Configuration.READER_TYPE_REMOTE_ENCRYPTED){
			return Configuration.getEncryptedRemoteFile();
		}
		
		return null;
	}
	
	public static ConfigurationFile getLocalFile() throws InvalidPropertiesFormatException, FileNotFoundException, IOException{
		return new LocalConfigurationFile(new File(Application.ConfigurationFilePath), Application.ConfigurationFileType);
	}
	
	public static ConfigurationFile getEncryptedLocalFile() throws InvalidPropertiesFormatException, FileNotFoundException, IOException{
		return new EncryptedLocalConfigurationFile(new File(Application.ConfigurationFilePath), Application.ConfigurationFileType);
	}
	
	public static ConfigurationFile getRemoteFile() throws InvalidPropertiesFormatException, IOException{
		return new RemoteConfigurationFile(new URL(Application.ConfigurationFileURI), Application.ConfigurationFileType);
	}
	
	public static ConfigurationFile getEncryptedRemoteFile() throws InvalidPropertiesFormatException, IOException{
		return new EncryptedRemoteConfigurationFile(new URL(Application.ConfigurationFileURI), Application.ConfigurationFileType);
	}
	
}
